//
//  AlertViewController.m
//  TotalDemo
//
//  Created by gujian on 16/5/5.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "AlertViewController.h"

typedef NS_ENUM(NSUInteger, BTN_TAG) {
    BTN_TAG_Normal = 301,
    BTN_TAG_Three,
    BTN_TAG_TextField,
    BTN_TAG_Sheet,
};

#define kTag    @"kTag"
#define kTitle  @"kTitle"

@interface AlertViewController ()

@end

@implementation AlertViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setupSubViews];

}

- (void)setupSubViews
{
    CGFloat margin = 30.0;
    
    NSArray *btnArr = @[@{kTag:@(BTN_TAG_Normal),kTitle:@"普通"},
                        @{kTag:@(BTN_TAG_Three),kTitle:@"三个按钮"},
                        @{kTag:@(BTN_TAG_TextField),kTitle:@"文本框"},
                        @{kTag:@(BTN_TAG_Sheet),kTitle:@"上拉菜单"},
                        ];
    
    CGFloat offY = margin;
    
    for (NSDictionary *dict in btnArr) {
        
        BTN_TAG tag = [dict[kTag] integerValue];
        NSString *title = dict[kTitle];
        
        UIButton *alertBtn = [TDViewUtils createCustomButton];
        alertBtn.tag = tag;
        [alertBtn setTitle:title forState:UIControlStateNormal];
        [alertBtn addTarget:self action:@selector(alertAction:) forControlEvents:UIControlEventTouchUpInside];
        alertBtn.frame = CGRectMake(margin, offY, SCREEN_WIDTH-2*margin, margin);
        [self.view addSubview:alertBtn];
        
        offY += (alertBtn.height + margin);
        
    }
    
    

}

- (void)alertAction:(UIButton *)btn
{
    switch (btn.tag) {
        case BTN_TAG_Normal:
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提醒" message:@"我是UIAlertController" preferredStyle:UIAlertControllerStyleAlert];
            __weak typeof(UIAlertController*) wsAlertController = alertController;
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:cancelAction];
            
            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:confirmAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
            
            break;
            
        case BTN_TAG_Three:
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"三个按钮" message:@"我是UIAlertController" preferredStyle:UIAlertControllerStyleAlert];
            __weak typeof(UIAlertController*) wsAlertController = alertController;
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:cancelAction];
            
            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:confirmAction];
            
            UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:resetAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
        case BTN_TAG_TextField:
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"我是UIAlertController" preferredStyle:UIAlertControllerStyleAlert];
            __weak typeof(UIAlertController*) wsAlertController = alertController;
            
            
            [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                textField.placeholder = @"用户名";
            }];
            
            [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
                textField.placeholder = @"密码";
                textField.secureTextEntry = YES;
            }];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:cancelAction];
            
            UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSString *userName = wsAlertController.textFields.firstObject.text;
                NSString *userPwd = wsAlertController.textFields.lastObject.text;
                LotLog(@"userName:%@\nuserPWd:%@",userName,userPwd);
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:confirmAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
            
            break;
            
        case BTN_TAG_Sheet:
        {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择菜单" message:@"我是UIAlertController" preferredStyle:UIAlertControllerStyleActionSheet];
            __weak typeof(UIAlertController*) wsAlertController = alertController;
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:cancelAction];
            
            UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"第一个" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:firstAction];
            
            UIAlertAction *secAction = [UIAlertAction actionWithTitle:@"第二个" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [wsAlertController dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:secAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
            
            break;
            
    }

}

@end
